let currentPage = 1;
let totalPage = 1;
$(function () {
    currentPage = 1;
    $.ajax({
        url: "/blog/getBlogBypage/",
        data: {"pageNum": currentPage},
        type: "POST",
        success: function (data) {
            for (let i = 0; i < data.length; i++) {
                $(".list-item").append("<div class='item'>\n" +
                    "                <div class='layui-fluid'>\n" +
                    "                    <div class='layui-row'>\n" +
                    "                        <div class='layui-col-xs12 layui-col-sm4 layui-col-md5'>\n" +
                    "                            <div class='img'><img th:src="+data[i].image+" alt=''></div>\n" +
                    "                        </div>\n" +
                    "                        <div class='layui-col-xs12 layui-col-sm8 layui-col-md7'>\n" +
                    "                            <div class='item-cont'>\n" +
                    "                                <h3>" + data[i].title + "<br><p style='color: grey;font-size: 10px'> 发表于：" + formatDate(data[i].createTime) + "&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp" + data[i].blogType.typeName + " </p></h3>\n" +
                    "                                <p>摘要：\n" +
                    "                                    " + data[i].keyWord + "</p>\n" +
                    "                                <a href='/details?Tid=" + data[i].id + " ' style='position: absolute;text-decoration:none; '>阅读更多...</a>\n" +
                    "                            </div>\n" +
                    "                        </div>\n" +
                    "                    </div>\n" +
                    "                </div>\n" +
                    "            </div>")
            }
            $(".list-item").append("<div id=currentPage=" + currentPage + "></div>")
        }
    })
    $.ajax({
        url: "/blog/getBlogNum",
        type: "POST",
        success: function (data) {
            totalPage = data;
        }
    })


})

function addBlog() {
    if (currentPage <= totalPage) {
        currentPage = currentPage + 1;
        $.ajax({
            url: "/blog/getBlogBypage/",
            data: {"pageNum": currentPage},
            type: "POST",
            success: function (data) {
                for (let i = 0; i < data.length; i++) {
                    $(".list-item").append("<div class='item'>\n" +
                        "                <div class='layui-fluid'>\n" +
                        "                    <div class='layui-row'>\n" +
                        "                        <div class='layui-col-xs12 layui-col-sm4 layui-col-md5'>\n" +
                        "                            <div class='img'><img src='https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1602153724885&di=aca2b66b652b1d4810c0c857bdf42f3b&imgtype=0&src=http%3A%2F%2Fattach.bbs.miui.com%2Fforum%2F201110%2F07%2F082726lghakmgjmgnah9aa.jpg' alt=''></div>\n" +
                        "                        </div>\n" +
                        "                        <div class='layui-col-xs12 layui-col-sm8 layui-col-md7'>\n" +
                        "                            <div class='item-cont'>\n" +
                        "                                <h3>" + data[i].title + "<br><p style='color: grey;font-size: 10px'> 发表于：" + formatDate(data[i].createTime) + "&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp" + data[i].blogType.typeName + " </p></h3>\n" +
                        "                                <p>摘要：\n" +
                        "                                    " + data[i].keyWord + "</p>\n" +
                        "                                <a href='/details?Tid=" + data[i].id + " ' style='position: absolute;text-decoration:none; '>阅读更多...</a>\n" +
                        "                            </div>\n" +
                        "                        </div>\n" +
                        "                    </div>\n" +
                        "                </div>\n" +
                        "            </div>")
                }
                pageId = "#currentPage=" + (currentPage - 1)
                location.href = pageId;
                $(".list-item").append("<div  id=currentPage=" + currentPage + "></div>")
            }
        })
    }else {
        Swal.fire({
            icon: 'warning',
            title: '已经是最后一页啦ε=( o｀ω′)ノ  ',
        })
    }

}


//时间戳转换方法    date:时间戳数字
function formatDate(date) {
    var date = new Date(date);
    var YY = date.getFullYear() + '-';
    var MM = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
    var DD = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate());
    var hh = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
    var mm = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
    var ss = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
    return YY + MM + DD + " " + hh + mm + ss;
}